package com.xiaohai.big_event_system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xiaohai.big_event_system.common.R;
import com.xiaohai.big_event_system.projo.Category;
import com.xiaohai.big_event_system.service.ICategoryService;
import com.xiaohai.big_event_system.utils.ThreadLocalUtil;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 分类
 * 前端控制器
 *
 * @author author
 * @since 2025-01-15
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private ICategoryService categoryService;

    /**
     * 添加分类
     * 添加分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody @Validated(Category.Add.class) Category category) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        category.setCreateUser(id);
        categoryService.save(category);
        return R.success(null, "添加成功");
    }

    /**
     * 分类列表
     * 根据条件查询
     *
     * @param currentPage 1
     * @param pageSize    10
     * @param category
     * @return
     */
    @GetMapping
    public R<IPage<Category>> list(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "10") Integer pageSize, Category category) {
        IPage<Category> list = categoryService.list(currentPage, pageSize, category);
        return R.success(list, "数据获取成功");
    }

    /**
     * 分类详情
     * 根据id查询详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public R<Category> detail(@RequestParam Integer id) {
        Category category = categoryService.getById(id);
        return R.success(category, "数据获取成功");
    }

    /**
     * 分类修改
     * 根据id修改
     *
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody @Validated(Category.Update.class) Category category) {
        categoryService.updateById(category);
        return R.success(null, "修改成功");
    }

    /**
     * 分类删除
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam @NotNull Integer id) {
        categoryService.removeById(id);
        return R.success(null, "删除成功");
    }
}
